package com.zx.onlineeducation.common.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果类
 *
 * @author 郑雪
 * @since 2022-01-07
 */
@Getter
@Setter
@ApiModel(value = "R对象", description = "返回结果")
public class R {

    @ApiModelProperty("是否成功")
    private Boolean success;

    @ApiModelProperty("返回码")
    private Integer code;

    @ApiModelProperty("返回消息")
    private String message;

    @ApiModelProperty("返回数据")
    private Map<String, Object> data = new HashMap<>();

    /**
     * 构造器私有，不可以直接创建
     */
    private R(){}

    /**
     * 返回成功，携带消息“成功”，且不带数据
     *
     * @return com.zx.onlineeducation.common.util.R
     * @author 郑雪
     * @since 2022-01-07
     */
    public static R ok() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    /**
     * 返回失败，携带消息“失败”，且不带数据
     *
     * @return com.zx.onlineeducation.common.util.R
     * @author 郑雪
     * @since 2022-01-07
     */
    public static R error() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.FAILED.getSuccess());
        r.setCode(ResultCodeEnum.FAILED.getCode());
        r.setMessage(ResultCodeEnum.FAILED.getMessage());
        return r;
    }

    /**
     * 根据状态码返回相应的结果，不带数据
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param resultCodeEnum 状态码
     * @author 郑雪
     * @since 2022-01-07
     */
    public static R resultCode(ResultCodeEnum resultCodeEnum) {
        R r = new R();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    /**
     * 返回成功，携带消息“成功”，且携带数据
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param data 要返回的数据
     * @author 郑雪
     * @since 2022-01-07
     */
    public static R successWithData(Map<String, Object> data) {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }

    /**
     * ------------使用链式编程，返回类本身----------
     * 自定义返回数据
     *
     * @param data 要返回的数据
     * @return com.zx.onlineeducation.common.util.R
     * @author 郑雪
     * @since 2022-01-07
     */
    public R data(Map<String, Object> data) {
        this.setData(data);
        return this;
    }

    /**
     * 设置返回数据
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param key 键的名称
     * @param value 键对应的值
     * @author 郑雪
     * @since 2022-01-07
     */
    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    /**
     * 自定义返回结果是否成功
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param success 是否成功（true成功，false失败）
     * @author 郑雪
     * @since 2022-01-08
     */
    public R success(Boolean success) {
        this.success = success;
        return this;
    }

    /**
     * 自定义返回结果中的返回码
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param code 返回码
     * @author 郑雪
     * @since 2022-01-08
     */
    public R code(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * 自定义返回结果中的返回消息
     *
     * @return com.zx.onlineeducation.common.util.R
     * @param message 返回消息
     * @author 郑雪
     * @since 2022-01-08
     */
    public R message(String message) {
        this.message = message;
        return this;
    }
}
